<!-- src/components/user/UserList.vue -->
<template>
  <div class="user-list-container">
    <el-card class="box-card">
      <template #header>
        <div class="clearfix">
          <span>用户列表</span>
          <el-button style="float: right; padding: 3px 0" type="primary" @click="handleCreate">
            新增用户
          </el-button>
        </div>
      </template>
      <div class="search-container">
        <el-input v-model="searchKey" placeholder="请输入用户名" @keyup.enter="handleSearch" clearable>
          <template #append>
            <el-button @click="handleSearch">搜索</el-button>
          </template>
        </el-input>
      </div>

      <el-table :data="userList" stripe border @row-click="handleRowClick">
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="username" label="用户名" width="120" />
        <el-table-column prop="real_name" label="姓名" width="120" />
        <el-table-column prop="email" label="邮箱" width="180" />
        <el-table-column prop="phone" label="电话" width="150" />
        <el-table-column prop="create_time" label="创建时间" width="180">
          <template #default="scope">
            {{ formatDate(scope.row.create_time) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template #default="scope">
            <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 50]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>
<script setup>
import { ref, onMounted, computed } from 'vue';
import { useUserStore } from '@/stores/user';
import { getUserList } from '@/api/user';
import dayjs from 'dayjs';
const userStore = useUserStore();
const userList = ref([]);
const total = ref(0);
const currentPage = ref(1);
const pageSize = ref(10);
const searchKey = ref('');

// 获取用户列表
const fetchUserList = async () => {
  try {
    const params = {
      page: currentPage.value,
      size: pageSize.value,
      username: searchKey.value
    };
    const result = await getUserList(params);
    userList.value = result.records;
    total.value = result.total;
  } catch (error) {
    console.error('获取用户列表失败', error);
  }
};
const formatDate = (date) => {
  return dayjs(date).format('YYYY-MM-DD HH:mm:ss');
};
const handleSearch = () => {
  currentPage.value = 1;
  fetchUserList();
};
const handleSizeChange = (newSize) => {
  pageSize.value = newSize;
  fetchUserList();
};
const handleCurrentChange = (newPage) => {
  currentPage.value = newPage;
  fetchUserList();
};

// 操作相关方法
const handleCreate = () => {
  userStore.setCurrentUser(null);
  userStore.setDialogVisible(true);
  userStore.setIsEdit(false);
};
const handleEdit = (row) => {
  userStore.setCurrentUser(row);
  userStore.setDialogVisible(true);
  userStore.setIsEdit(true);
};
const handleDelete = async (row) => {
  try {
    await userStore.deleteUser(row.id);
    fetchUserList();
  } catch (error) {
    console.error('删除用户失败', error);
  }
};

const handleRowClick = (row) => {
  userStore.setCurrentUser(row);
  userStore.setDetailVisible(true);
};
onMounted(() => {
  fetchUserList();
});
</script>
<style scoped>
.search-container {
  margin-bottom: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>